﻿using System;
using System.Linq;
using System.Web.Security;
using CP_SchoolForChildren_v0._1.Models.DataModel;

namespace CP_SchoolForChildren_v0._1.Controllers.Support 
{
    public class SFCRoleProvider : RoleProvider
    {
        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            throw new NotImplementedException();
        }

        public override string[] GetRolesForUser(string username)
        {
            using (var db = new SFCEntities())
            {
                var user = db.Users.FirstOrDefault(u => u.UserName.Equals(username, StringComparison.CurrentCultureIgnoreCase));
                var role = from ur in db.UserRoles
                           where user.RoleID == ur.RoleID
                           select ur.RoleName;
                if (role != null)
                    return role.ToArray();
                else
                    return new string[] { }; ;
            }
           // return _repository.UserRepository.GetRoleForUser(username);
        }

        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            using (SFCEntities db = new SFCEntities())
            {
                var user = db.Users.FirstOrDefault(u => u.UserName.Equals(username, StringComparison.CurrentCultureIgnoreCase));
                var role = from ur in db.UserRoles
                           where user.RoleID == ur.RoleID
                           select ur.RoleName;
                if (user != null)
                    return role.Any(r => r.Equals(roleName, StringComparison.CurrentCultureIgnoreCase));
                else
                    return false;
            }
            //var user = _repository.UserRepository.GetUserByUserName(username);

            //return user != null && GetRolesForUser(username).Contains(roleName);
             
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            throw new NotImplementedException();
        }
    }
}